import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index.js'
import {
  getLocal
} from '@/utils/local.js'
import {
  apiGetUser
} from '@/api/comment.js'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('@/views/login/login.vue'),
    meta: {
      title:'知行校园网'
    }
  },
  {
    path: '/home',
    component: () => import('@/views/home/home.vue'),
    redirect: '/home/first',
    meta: {
      title:'校园头条'
    },
    children: [{
        path: 'first',
        component: () => import('@/views/home/first/index.vue'),
        meta: {
          needTab: true,
          title:'校园头条'
        }
      },
      {
        path: 'search',
        component: () => import('@/views/home/search/search.vue'),
        meta: {
          needTab: true,
          title:'搜索'
        }
      },
      {
        path: 'searchresult',
        component: () => import('@/views/home/search/searchresult.vue'),
        meta: {
          title:'搜索结果'
        }
      },
      {
        path: 'detail/:article_id?',
        component: () => import('@/views/home/search/detail.vue'),
        meta: {
          title: '文章详情'
        }
      },
      {
        path: 'video',
        component: () => import('@/views/home/videoPage/video.vue'),
        meta: {
          needTab: true,
          title:'视频'
        }
      },
      {
        path: 'my',
        component: () => import('@/views/home/my/my.vue'),
        meta: {
          needTab: true,
          title: '个人主页'
        }
      },
      {
        path: 'editInfo/:target?',
        component: () => import('@/views/home/my/childComps/editInfo.vue'),
        meta: {
          needLogin: true,
          title: '个人中心'
        }
      },
      {
        path: 'edit/:target?',
        component: () => import('@/views/home/my/childComps/edit.vue'),
        meta: {
          needLogin: true,
          title: '编辑'
        }
      },
      {
        path: 'system/:target?',
        component: () => import('@/views/home/my/childComps/system.vue'),
        meta: {
          needLogin: true,
          title: '系统设置'
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 前置守卫
router.beforeEach((to, from, next) => {
  // 修改页面标题
  document.title = to.meta.title
  // 每次跳转路由取消所有请求
  window.cancelAxios('', true)
  const _scrollTop =
    document.documentElement.scrollTop ||
    document.body.scrollTop ||
    window.pageYOffset
  from.meta.scrollTop = _scrollTop
  if (to.meta.needLogin) {
    if (store.state.isLogin) {
      next()
    } else {
      if (getLocal('token')) {
        apiGetUser().then(res => {
          store.commit('setUserInfo', res.data)
          store.commit('setIsLogin', true)
          next()
        })
      } else {
        // 登录页跳转返回前面退出的页面
        next('/login?redirect=' + to.fullPath)
      }
    }
  } else {
    if (getLocal('token') && !store.state.isLogin) {
      // to do..
      apiGetUser().then(res => {
        // console.log(res)
        store.commit('setUserInfo', res.data)
        store.commit('setIsLogin', true)
      })
    }
    next()
  }
})
router.afterEach(() => {
  window.scrollTo(0, 0)
})
// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
